/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.andrea.girardello.view;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import net.andrea.girardello.Midlet;
import net.andrea.girardello.controller.WebcamConnector;

/**
 *
 * TODO: two modes, movie and refresh
 * @author agirardello
 */
public class CamView extends Form implements CommandListener {

    private Midlet main;
    private ImageItem imageItem;
    private Timer timer;
    
    private Command refreshCommand;
    private Command autoRefreshCommand;
    private Command stopAutoRefreshCommand;
    private Command exitCommand;
    
    public CamView(String title, Midlet main) {
        super(title);
        this.main = main;
        setupItems();
        refresh();
    }

    private void setupItems() {
        
        imageItem = new ImageItem(null, null, ImageItem.LAYOUT_NEWLINE_BEFORE, "No image");
        imageItem.setLayout(ImageItem.LAYOUT_LEFT);
        this.append(imageItem);

        exitCommand = new Command("Exit", Command.EXIT, 0);
        this.addCommand(exitCommand);
        this.setCommandListener(this);
        
        refreshCommand = new Command("Refresh", Command.OK, 1);
        this.addCommand(refreshCommand);
        this.setCommandListener(this);
        
        autoRefreshCommand = new Command("Auto-refresh", Command.OK, 2);
        this.addCommand(autoRefreshCommand);
        this.setCommandListener(this);
        
        stopAutoRefreshCommand = new Command("Stop auto-refresh", Command.OK, 2);
        this.setCommandListener(this);
    }
    
    public void commandAction(Command command, Displayable arg1) {
        if (command == exitCommand){
            main.destroyApp(true);
        } else if (command == refreshCommand){
            refresh();
        } else if (command == autoRefreshCommand) {
            autoRefresh(true);
            this.addCommand(stopAutoRefreshCommand);
            this.removeCommand(autoRefreshCommand);
        } else if (command == stopAutoRefreshCommand) {
            autoRefresh(false);
            this.addCommand(autoRefreshCommand);
            this.removeCommand(stopAutoRefreshCommand);
        }
    }
    
    private void refresh() {
        //put a loading bar!
        WebcamConnector connector = new WebcamConnector(this);
        Thread t = new Thread(connector);
        t.start();
    }

    private void autoRefresh(boolean what) {
        if (what) {
            timer = new Timer();
            WebcamConnector connector = new WebcamConnector(this);
            timer.scheduleAtFixedRate(connector,0, 500);
        } else {
            timer.cancel();
        }
    }
    
    public void setImage(Image image) {
        imageItem.setImage(image);
    }
}
